﻿using System.ComponentModel;
using System.Drawing.Drawing2D;
using Tetris.Game.ControlsBase.Common;

namespace Tetris.Game.ControlsBase
{
    [ToolboxItem(false)]
    public class TetrisShapeBase : Control, IGameProperty
    {
        public TetrisShapeBase()
        {
            SetStyle(
                ControlStyles.ResizeRedraw
                | ControlStyles.DoubleBuffer
                | ControlStyles.UserPaint
                | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.SupportsTransparentBackColor,
                true
                );
            this.UpdateStyles();
            Size = new Size(72, 72);
            Margin = new Padding(0, 0, 0, 0);
            Padding = new Padding(0, 0, 0, 0);
            BackColor = Color.Transparent;
            Id = Guid.NewGuid().ToString();
            IsMove = true;
        }

        private ShapeType _shapeType = ShapeType.O;
        [Description("形状类型"), Category("TF.Game"), Browsable(true)]
        [DefaultValue(ShapeType.O)]
        public ShapeType TetrisShapeType
        {
            get { return _shapeType; }
            set
            {
                _shapeType = value;
                //this.Invalidate();
            }
        }
        private ShapeColor _shapeColor = ShapeTypeEx.OShapeColor;
        [Description("形状颜色"), Category("TF.Game"), Browsable(true)]
        [DefaultValue(typeof(ShapeColor))]
        public ShapeColor TetrisShapeColor
        {
            get { return _shapeColor; }
            set
            {
                _shapeColor = value;
                this.Invalidate();
            }
        }
        private int _shapePadding = 0;
        [Description("小方框内边距"), Category("TF.Game"), Browsable(true)]
        [DefaultValue(0)]
        public int ShapePadding
        {
            get { return _shapePadding; }
            set
            {
                _shapePadding = value;
                this.Invalidate();
            }
        }
        private int _trapezoidWidth = 8;
        [Description("梯形宽"), Category("TF.Game"), Browsable(true)]
        [DefaultValue(8)]
        public int TrapezoidWidth
        {
            get { return _trapezoidWidth; }
            set
            {
                _trapezoidWidth = value;
                this.Invalidate();
            }
        }
        [Browsable(false)]
        public int BlockSize { get; protected set; }
        [Browsable(false)]
        public bool IsMove { get; set; }
        public string Id { get; set; }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            Graphics g = pevent.Graphics;
            Color parentBackColor = Parent?.BackColor ?? Color.Transparent;
            using (var brush = new SolidBrush(parentBackColor))
            {
                g.FillRectangle(brush, ClientRectangle);
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            PaintBlocks(g);
        }
        protected virtual void PaintBlocks(Graphics g)
        {
        }
        protected virtual void PaintBlock(Graphics g, int x, int y, int width, int height)
        {
            using Pen pen = new Pen(TetrisShapeColor.FrameTrapezoidColor, 2);

            // 外正方形
            Rectangle rect = new Rectangle(x, y, width, height);
            using (SolidBrush brush = new SolidBrush(TetrisShapeColor.OuterColor))
            {
                g.FillRectangle(brush, rect);

            }

            // 上
            Point[] points = new Point[4];
            points[0] = new Point(x + TrapezoidWidth - ShapePadding, y + TrapezoidWidth - ShapePadding);// 左下
            points[1] = new Point(x + width - TrapezoidWidth + ShapePadding, y + TrapezoidWidth - ShapePadding);// 右下
            points[2] = new Point(x + width - ShapePadding, y + ShapePadding);// 右上
            points[3] = new Point(x + ShapePadding, y + ShapePadding);// 左上
            Rectangle rectangle = new Rectangle(points[3].X, points[3].Y, points[2].X - points[3].X, points[1].Y - points[3].Y);
            using (LinearGradientBrush brush = new LinearGradientBrush(rectangle, TetrisShapeColor.OuterColor, TetrisShapeColor.UpperTrapezoidColor, LinearGradientMode.Vertical))
            {
                g.FillPolygon(brush, points);
            }

            // 下
            Point[] points2 = new Point[4];
            points2[0] = new Point(x + ShapePadding, y + height - ShapePadding);
            points2[1] = new Point(x + width - ShapePadding, y + height - ShapePadding);
            points2[2] = new Point(x + width - TrapezoidWidth + ShapePadding, y + height - TrapezoidWidth + ShapePadding);
            points2[3] = new Point(x + TrapezoidWidth - ShapePadding, y + height - TrapezoidWidth + ShapePadding);
            Rectangle rectangle2 = new Rectangle(points2[3].X, points2[3].Y, points2[2].X - points2[3].X, points2[1].Y - points2[3].Y);
            using (LinearGradientBrush brush = new LinearGradientBrush(rectangle2, TetrisShapeColor.UnderTrapezoidColor, TetrisShapeColor.OuterColor, LinearGradientMode.Vertical))
            {
                g.FillPolygon(brush, points2);
            }

            // 左
            Point[] points3 = new Point[4];
            points3[0] = new Point(x + ShapePadding, y + height - ShapePadding);
            points3[1] = new Point(x + TrapezoidWidth - ShapePadding, y + height - TrapezoidWidth + ShapePadding);
            points3[2] = new Point(x + TrapezoidWidth - ShapePadding, y + TrapezoidWidth - ShapePadding);
            points3[3] = new Point(x + ShapePadding, y + ShapePadding);
            Rectangle rectangle3 = new Rectangle(points3[3].X, points3[3].Y, points3[2].X - points3[3].X, points3[1].Y - points3[3].Y);
            using (LinearGradientBrush brush = new LinearGradientBrush(rectangle3, TetrisShapeColor.OuterColor, TetrisShapeColor.LeftTrapezoidColor, LinearGradientMode.Horizontal))
            {
                g.FillPolygon(brush, points3);
            }

            // 右
            Point[] points4 = new Point[4];
            points4[0] = new Point(x + width - TrapezoidWidth + ShapePadding, y + height - TrapezoidWidth + ShapePadding);
            points4[1] = new Point(x + width - ShapePadding, y + height - ShapePadding);
            points4[2] = new Point(x + width - ShapePadding, y + ShapePadding);
            points4[3] = new Point(x + width - TrapezoidWidth + ShapePadding, y + TrapezoidWidth - ShapePadding);
            Rectangle rectangle4 = new Rectangle(points4[3].X, points4[3].Y, points4[2].X - points4[3].X, points4[1].Y - points4[3].Y);
            using (LinearGradientBrush brush = new LinearGradientBrush(rectangle4, TetrisShapeColor.RightTrapezoidColor, TetrisShapeColor.OuterColor, LinearGradientMode.Horizontal))
            {
                g.FillPolygon(brush, points4);
            }

            // 内正方形
            Rectangle rect2 = new Rectangle(x + TrapezoidWidth, y + TrapezoidWidth, width - TrapezoidWidth - TrapezoidWidth, height - TrapezoidWidth - TrapezoidWidth);
            using SolidBrush brush2 = new SolidBrush(TetrisShapeColor.InnerColor);
            g.FillRectangle(brush2, rect2);
        }
    }
}
